package com.flin.icesilkworm.examination.course.populator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.flin.icesilkworm.examination.bas.constant.ITheBasicExamineeConstant;
import com.flin.icesilkworm.examination.bas.constant.ITheExamineeGroupRelConstant;
import com.flin.icesilkworm.examination.bas.model.TheBasicExaminee;
import com.flin.icesilkworm.examination.bas.model.TheExamineeGroupRel;
import com.flin.icesilkworm.examination.bas.po.TheBasicExamineeDo;
import com.flin.icesilkworm.examination.bas.po.TheExamineeGroupRelDo;
import com.flin.icesilkworm.examination.course.constant.ICourseProjectsRelConstant;
import com.flin.icesilkworm.examination.course.exp.CourseProjectsExample;
import com.flin.icesilkworm.examination.course.model.CourseProjectsRel;
import com.flin.icesilkworm.examination.course.po.CourseProjectsRelDo;
import com.flin.icesilkworm.examination.gen.course.populator.GeneratedCourseProjectsPopulator;
import com.flin.icesilkworm.framework.jdbc.interceptors.JdbcExampleSupport;
import com.flin.icesilkworm.framework.jdbc.objects.StringAttribute;
import com.flin.icesilkworm.framework.spring.SpringWebUtils;
import com.flin.icesilkworm.framework.ui.pojo.standard.StandardUIItemForm;

public class CourseProjectsPopulator extends GeneratedCourseProjectsPopulator  {


	private static CourseProjectsPopulator instance = new CourseProjectsPopulator();
	

	public static CourseProjectsPopulator newInstance() {
		return instance;
	}
	
	//针对每一个查询条件组装方法
	protected void loadExampleByKey(JdbcExampleSupport<?> example, String key,
			StringAttribute fieldAttr, StandardUIItemForm item) {
		
		CourseProjectsExample exp = (CourseProjectsExample)example;
		
		//
		
	}
	
	
	//条件组装后，需要进行默认判断是否需要扩展
	protected boolean defaultDealExample(CourseProjectsExample example,Map<String,String> additions) {
		
		if(additions.containsKey("useExam")){//启动组装应试逻辑
			long userid = SpringWebUtils.getUserId();
			TheBasicExaminee examinee = TheBasicExamineeDo.get().exameeId(userid).deleted(false).re()
				.and(t->t.setAttribute(ITheBasicExamineeConstant.DUTY_ID)).findForWhere();
			
			List<TheExamineeGroupRel> groups = TheExamineeGroupRelDo.get().examId(userid).re()
				.and(t->t.setAttribute(ITheExamineeGroupRelConstant.GROUP_ID)).queryForWhere();
			
			List<CourseProjectsRel> rels = CourseProjectsRelDo.get().examId(userid).re()
			    .and(t->t.setAttribute(ICourseProjectsRelConstant.PRO_ID,ICourseProjectsRelConstant.AUTH_TYPE)).queryForWhere();
			
			if(examinee != null){
				example.and("replace(replace(%{0}.duty_ids,'[',','),']',',') like CONCAT('%,'," + examinee.getDutyId() + ",',%')");
			}
			
			if(groups != null && groups.size() > 0){
				
				for(TheExamineeGroupRel group : groups){
					example.and("replace(replace(%{0}.group_ids,'[',','),']',',')  like CONCAT('%,'," + group.getGroupId() + ",',%')").or(0);
				}
			}
			
			if(rels != null && rels.size() > 0){
				List<Long> include = new ArrayList<>();
				List<Long> exclude = new ArrayList<>();
				rels.forEach(t->{
					if(ICourseProjectsRelConstant.AUTH_TYPE_CONTAINS.equals(t.getAuthType())){
						include.add(t.getProId());
					} else if(ICourseProjectsRelConstant.AUTH_TYPE_EXCLUDE.equals(t.getAuthType())){
						exclude.add(t.getProId());
					}
					
				});
				if(include.size() > 0){
					example.or(true, t->t.andProIdIn(include));
				}
				if(exclude.size() > 0){
					example.andProIdNotIn(exclude);
				}
			}
 
		}
		
		return super.defaultDealExample(example,additions);//true,直接结束，不做后续的自动条件增加，false:不做直接终止
	}
	
	//遇到别名时组装方式
	public JdbcExampleSupport<?> joinExample(String key, JdbcExampleSupport<?> example,String op,Map<String,String> params) {
		
		return super.joinExample(key,example,op,params);
	}
	
	//针对每个查询条件的自定义条件
	public boolean beforeLoadExample(JdbcExampleSupport<?> example,String key, StringAttribute fieldAttr,
			String op, StandardUIItemForm item,Map<String,String> params) {
		
		return false;
	}
																																																																																																
}